package com.xjj.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjj.entity.Discuss;
import com.xjj.entity.Parent;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface DiscussMapper extends BaseMapper<Discuss> {
    //查当前相册下的所有评论(其中子评论要包含父评论的信息目前需要id、name、content)
    @Select("select * from  xjj_discuss where photo_id=#{id} order by floor")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "sender_id",property = "senderId"),
            @Result(column = "sender_id", property = "parent", one = @One(select = "com.xjj.mapper.DiscussMapper.findParentBySenderId")),
            @Result(column = "pid",property = "pDiscuss",one=@One(select = "com.xjj.mapper.DiscussMapper.findByPid"))
    })
    List<Discuss> findAllDisscussByPhotoId(int id);
    @Select("select * from xjj_discuss where id = #{pid}")
    @Results({
            @Result(column = "sender_id", property = "parent", one = @One(select = "com.xjj.mapper.DiscussMapper.findParentBySenderId")),
    })
    Discuss findByPid(int pid);

    //根据发送者id查询家长信息
    @Select("select * from xjj_parent where id = #{senderId}")
    Parent findParentBySenderId(int senderId);

    //查询floor最大值
    @Select("select max(floor) from xjj_discuss")
    int findMaxFloor();

    //发布评论
    @Insert("insert into xjj_discuss values(default,#{content},#{senderId},#{photoId},#{pid},#{time},#{floor})")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void add(Discuss discuss);

    //查询一条评论
    @Select("select * from xjj_discuss where pid = 0 and floor=#{floor}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "sender_id", property = "parent", one = @One(select = "com.xjj.mapper.DiscussMapper.findParentBySenderId")),
    })
    Discuss findByFloor(int floor);

    //根据id查询数据
    @Select("select * from xjj_discuss where id = #{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "pid",property = "pid"),
            @Result(column = "sender_id",property = "senderId"),
            /*@Result(column = "sender_id", property = "parent", one = @One(select = "com.xjj.mapper.DiscussMapper.findParentBySenderId")),
            @Result(column = "pid",property = "pDiscuss",one=@One(select = "com.xjj.mapper.DiscussMapper.findByPid"))*/
    })
    Discuss findById(int id);

    //删除评论
    @Delete("delete from xjj_discuss where id = #{discussId} or pid = #{discussId}")
    void deleteById(int discussId);


/*    //删除子评论
    @Delete("delete from xjj_discuss where pid = #{id}")
    void deleteByPid(int id);*/


    /*
    //根据相册id查询一级评论
    @Select("select * from xjj_discuss where photo_id = #{id} and pid is null order by time")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "id",property = "subDiscuss",many=@Many(select="com.xjj.mapper.DiscussMapper.findSubDiscussById")),
            @Result(column = "sender_id", property = "parent", one = @One(select = "com.xjj.mapper.DiscussMapper.findParentBySenderId")),
    })
    List<Discuss> findByPhotoId(int id);
    //根据一级评论查询子评论
    @Select("select * from xjj_discuss where pid = #{pid} order by time")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "sender_id", property = "parent", one = @One(select = "com.xjj.mapper.DiscussMapper.findParentBySenderId")),
    })
    List<Discuss> findSubDiscussById(int pid);*/
}
